
function loadAll(){

    var DELAY = 3500;
    var FADEOUT = 1000;
    var ok = 'ok';

    $('.alert').delay(DELAY).fadeOut(FADEOUT);

    $('#newPost').click(function(){

        var userId = $('#userId');
        var threadId = $('#threadId');
        var text = $('#new-textarea');
        var errorm = $('#error-insert-message');

        $.ajax({

            type    : 'POST',
            url     : '../../posts/addPost',
            data    : {user: userId.val(), thread: threadId.val(), text: text.val()},
            success : function(data){

                if(data !== ok){

                    errorm.html(data)
                    $('#error-insert-post').fadeIn().delay(DELAY).fadeOut(FADEOUT);
                }
                else{
                    errorm.html('');
                    text.val('');
                    location.reload();
                }
            }
        })
    });

    $('.edit-post').click(function(){

        var ref = $(this).attr('itemref');

        $('#postId').attr('value', ref);
        $('#edit-textarea').val($('#post' + ref + '-text').html())
    });

    $('#editPost').click(function(){

        var postId = $('#postId');
        var text = $('#edit-textarea');
        var errorm = $('#error-update-message');

        $.ajax({

            type    : 'POST',
            url     : '../../posts/editPost',
            data    : {post: postId.val(), text: text.val()},
            success : function(data){

                if(data !== ok){

                    errorm.html(data)
                    $('#error-update-post').fadeIn().delay(DELAY).fadeOut(FADEOUT);
                }
                else{
                    errorm.html('');
                    text.val('');
                    location.reload();
                }
            }
        })
    });

    $('.delete-post').click(function(){

        var post = $(this).attr('itemref');
        var thread = $(this).attr('threadref')

        var c = confirm($(this).attr('itemconfirm'));

        if(c){

            $.ajax({

                type    : 'POST',
                url     : '../../posts/deletePost',
                data    : {post: post, thread: thread},
                success : function(){

                    location.reload();
                }
            })
        }
    });

    $('.tag-select').click(tagAdd);

    function tagAdd(){

        var ref = $(this).attr('itemref');

        $(this).removeClass('tag-select');
        $(this).addClass('tag-remove');
        $(this).unbind('click');

        $('#tags-container').append($(this));
        // $('#tags-container').append('&nbsp;');

        $('#tags-container').append('<input id="tag-' + ref +'-hidden" type="hidden" value="' + ref + '" name="tags" />')

        $(this).click(tagDelete);
    }

    function tagDelete(){

        $(this).removeClass('tag-remove');
        $(this).addClass('tag-select');
        $(this).unbind('click');

        $('#tag-selection').append($(this));
        $('#tag-' + $(this).attr('itemref') +'-hidden').remove();

        $(this).click(tagAdd);
    }

    $('.course-delete').click(function(){


        var course = $(this).attr('itemref');
        var c = confirm($(this).attr('itemconfirm'));

        if(c){

            $.ajax({

                type    : 'POST',
                url     : 'courses/deleteCourse',
                data    : {course : course},
                success : function(data){

                    if(data == ok){

                        location.reload();
                    }
                }

            });
        }
    });

    $('.thread-delete').click(function(){

        var thread = $(this).attr('itemref');
        var c = confirm($(this).attr('itemconfirm'));

        if(c){

            $.ajax({

                type    : 'POST',
                url     : '../../threads/deleteThread',
                data    : {thread : thread},
                success : function(data){

                    if(data == ok){

                        location.reload();
                    }
                }

            });
        }
    });

    $('.vote-post').click(function(){

        var post = $(this).attr('itemref');

        $.ajax({

            type    : 'POST',
            url     : '../../votes/votePost',
            data    : {post : post},
            success : function(data){

                if(data == ok){

                    location.reload();
                }
            }
        });
    });

    $('.remove-vote-post').click(function(){

        var vote = $(this).attr('itemref');

        $.ajax({

            type    : 'POST',
            url     : '../../votes/removeVotePost',
            data    : {vote : vote},
            success : function(data){

                if(data == ok){

                    location.reload();
                }
            }
        });

    });
}